from application import db
from common.libs import Helper
from common.models.foods.FoodStockChangeLog import FoodStockChangeLog
from common.models.foods.Food import Food

class FoodService(object):

    @staticmethod
    def setStockChange(food_id, quantity=0, note=''):

        if food_id < 1:
            return False

        food = db.session.query(Food).filter_by(id=food_id).first()
        if not food:
            return False

        stockChangeModel = FoodStockChangeLog()
        stockChangeModel.food_id = food.id
        stockChangeModel.unit = quantity
        stockChangeModel.total_stock = food.stock
        stockChangeModel.note = note
        stockChangeModel.created_time = Helper.geneDatetime()
        db.session.add(stockChangeModel)
        db.session.commit()
        return True